/**
 * file
 */
package com.tdkj.action.backstage.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.tdkj.action.BaseAction;
import com.tdkj.model.common.BeanFile;
import com.tdkj.model.common.BeanPhotoCategory;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.common.IBeanFileService;
import com.tdkj.service.common.IBeanPhotoCategoryService;
import com.tdkj.service.user.IBeanUserService;
import com.tdkj.util.Page;

/**
 * 
 * FileName: AdminPhotoAction.java
 * 
 * @Description: TODO 用户相册管理
 * 
 *               All rights Reserved, Designed By ZTE-ITS
 * 
 *               Copyright: Copyright(C) 2010-2011
 * 
 *               Company 天成科技有限公司
 * 
 * @author: 李兴建
 * 
 * @version V1.0
 * 
 *          Createdate: 2012-8-6 下午09:09:12
 * 
 * 
 * 
 *          Modification History:
 * 
 *          Date Author Version Discription
 * 
 *          
 *          ----------------------------------------------------------------------
 *          -------------
 * 
 *          2012-8-6 李兴建 1.0 1.0
 * 
 *          Why & What is modified: <修改原因描述>
 */
public class UserPhotoAction extends BaseAction {
	private static final long serialVersionUID = -1699081076775720391L;
	private Long uid;//用户id
	private Long cid;//分类id
	private Long pid;//照片id
	Map<String,String> statusMap=new HashMap<String,String>();//是否已经审核
	private IBeanUserService userService;
	private IBeanPhotoCategoryService categoryService;
	private IBeanFileService fileService;
	private String[] ids;
	public String[] getIds() {
		return ids;
	}
	public void setIds(String[] ids) {
		this.ids = ids;
	}
	public Long getUid() {
		return uid;
	}

	public void setUid(Long uid) {
		this.uid = uid;
	}

	public Long getCid() {
		return cid;
	}

	public void setCid(Long cid) {
		this.cid = cid;
	}

	public Long getPid() {
		return pid;
	}

	public void setPid(Long pid) {
		this.pid = pid;
	}
	public Map<String, String> getStatusMap() {
		return statusMap;
	}
	public void setStatusMap(Map<String, String> statusMap) {
		this.statusMap = statusMap;
	}
	public IBeanUserService getUserService() {
		return userService;
	}
	@Resource
	public void setUserService(IBeanUserService userService) {
		this.userService = userService;
	}

	public IBeanPhotoCategoryService getCategoryService() {
		return categoryService;
	}
	@Resource
	public void setCategoryService(IBeanPhotoCategoryService categoryService) {
		this.categoryService = categoryService;
	}
	public IBeanFileService getFileService() {
		return fileService;
	}
	@Resource
	public void setFileService(IBeanFileService fileService) {
		this.fileService = fileService;
	}
	
	public String findAll(){
		String sql =" select ttt1.id,ttt1.username,ttt1.amount as allAmount,"+
					" ttt1.fsize,ttt2.amount as unAmount,ttt3.amount as amount"+
					" from"+
					" (select tt1.id,tt1.username,tt2.amount,tt2.fsize from"+
					"  xnky_user tt1 left join"+
					" (select t2.userid,count(t1.id) as amount,sum(t1.fsize)/(1024*1024) as fsize  "+
					"	from common_photocategory t2 left join common_file t1 on(t1.photocategoryid = t2.id) "+
					"	group by t2.userid) tt2 on (tt1.id=tt2.userid)"+
					" ) ttt1,"+
					" (select tt1.id,tt1.username,tt2.amount,tt2.fsize from"+
					"  xnky_user tt1 left join"+
					" (select t2.userid,count(t1.id) as amount,sum(t1.fsize)/(1024*1024) as fsize  "+
					"	from common_photocategory t2 left join (select * from common_file where status='未审核') t1 on(t1.photocategoryid = t2.id) "+
					"	group by t2.userid) tt2 on (tt1.id=tt2.userid)"+
					" ) ttt2,"+
					" (select tt1.id,tt1.username,tt2.amount,tt2.fsize from"+
					" xnky_user tt1 left join"+
					" (select t2.userid,count(t1.id) as amount,sum(t1.fsize)/(1024*1024) as fsize "+ 
					"	from common_photocategory t2 left join (select * from common_file where status='已审核') t1 on(t1.photocategoryid = t2.id) "+
					"	group by t2.userid) tt2 on (tt1.id=tt2.userid)"+
					" ) ttt3"+
					" where ttt1.id = ttt2.id and ttt2.id = ttt3.id ";
		this.queryByPage(sql, "userPhotoAction!findAll.action",10);
		return "index";
	}
	
	public String findCategorys(){
		BeanUser u=userService.findById(this.getUid(), BeanUser.class);
		uid=u.getId();
		String sql =" select ttt1.username,ttt1.name,"+
			" ttt1.id,ttt1.createtime,"+
			" ttt1.amount,"+
			" ttt1.fsize,"+
			" ttt2.amount as unAmount,"+
			" ttt3.amount as amount3 "+
			"   from "+
			" (select tt1.username,tt3.name,tt3.id,tt3.createtime,tt3.status,tt2.amount,(tt2.fsize)/(1024*1024) as fsize"+
			" from xnky_user tt1,"+
			" (select t2.userid,t2.id,count(t1.uuid) as amount,sum(t1.fsize) as fsize "+
			" from common_photocategory t2 left join common_file t1 on(t1.photocategoryid = t2.id) "+
			" group by t2.userid,t2.id)  tt2,common_photocategory tt3"+
			"  where tt1.id=tt2.userid and tt2.id=tt3.id and  tt1.id="+uid+") ttt1, "+
			" (select tt1.username,tt3.name,tt3.id,tt3.createtime,tt3.status,tt2.amount,(tt2.fsize)/(1024*1024) as fsize"+
			" from xnky_user tt1,"+
			" (select t2.userid,t2.id,count(t1.uuid) as amount,sum(t1.fsize) as fsize "+
			" from common_photocategory t2 left join (select * from common_file where status='未审核') t1 on(t1.photocategoryid = t2.id) "+
			" group by t2.userid,t2.id)  tt2,common_photocategory tt3"+
			"   where tt1.id=tt2.userid and tt2.id=tt3.id and  tt1.id="+uid+") ttt2,  "+
			"  (select tt1.username,tt3.name,tt3.id,tt3.createtime,tt3.status,tt2.amount,(tt2.fsize)/(1024*1024) as fsize"+
			" from xnky_user tt1,"+
			" (select t2.userid,t2.id,count(t1.uuid) as amount,sum(t1.fsize) as fsize "+
			" from common_photocategory t2 left join (select * from common_file where status='已审核') t1 on(t1.photocategoryid = t2.id) "+
			" group by t2.userid,t2.id)  tt2,common_photocategory tt3"+
			"   where tt1.id=tt2.userid and tt2.id=tt3.id and  tt1.id="+uid+") ttt3    "+
			" where ttt1.id = ttt2.id and ttt2.id = ttt3.id";
		this.queryByPage(sql, "userPhotoAction!findCategorys.action",10);
		return "category_list";
	}
	
	public String findPhotos(){
		cid=this.getCid();
		BeanPhotoCategory c=categoryService.findById(this.getCid(), BeanPhotoCategory.class);
		Criteria criteria = this.createCriteria(BeanFile.class);
		criteria.add(Restrictions.eq("photocategory",c));
		criteria.addOrder(Order.desc("createtime"));// 对状态进行排序
		this.queryByPage(criteria, "userPhotoAction!findPhotos.action", 10);
		return "photo_list";
	}
	
	//删除相册
	public String deleteCategory(){
		uid=this.getUid();
		categoryService.delete(this.getCid(), BeanPhotoCategory.class);
		return findCategorys();
	}
	//删除照片
	public String deletePhoto() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		String path=request.getRealPath("");
		path=path.replaceAll("\\\\", "/");
		cid=this.getCid();
		fileService.deletePhoto(this.getPid(),path);
		return findPhotos();
	}
	//审核照片
	public String updateStatus(){
		BeanFile f=fileService.findById(this.getPid(), BeanFile.class);
		f.setStatus("已审核");
		fileService.saveOrUpdate(f, BeanFile.class);
		cid=this.getCid();
		return findPhotos();
	}

	//批量删除分类
	public String deleteCategorys() {
		uid=this.getUid();
		List<Long> photoIds=new ArrayList<Long>();
		if (null != ids) {
			for (String idtemp : ids) {
				List<BeanFile> fs=categoryService.findById(Long.valueOf(idtemp), BeanPhotoCategory.class).getPhotos();
				for(int i=0;i<fs.size();i++){
					photoIds.add(fs.get(i).getId());
				}
				this.getBaseService().delete(Long.parseLong(idtemp), BeanPhotoCategory.class);
			}
		}
		return findCategorys();
	}
	//批量删除照片
	public String deletePhotos() throws NumberFormatException, Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String path=request.getRealPath("");
		path=path.replaceAll("\\\\", "/");
		if (null != ids) {
			for (String idtemp : ids) {
				fileService.deletePhoto(Long.valueOf(idtemp),path);
			}
		}
		cid=this.getCid();
		return findPhotos();
	}
	private void deleteAllFiles(List<Long> ids) throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		String path=request.getRealPath("");
		path=path.replaceAll("\\\\", "/");
		for(int i=0;i<ids.size();i++){
			fileService.deletePhoto(ids.get(i),path);
		}
	}

}
